// 节流函数
const throttle = <F extends (...args: any[]) => void>(
  func: F,
  delay: number
) => {
  let timer: ReturnType<typeof setTimeout> | null = null;
  return function (this: any, ...args: Parameters<F>) {
    if (!timer) {
      timer = setTimeout(() => {
        func.apply(this, args);
        timer = null;
      }, delay);
    }
  };
};

// 防抖函数
const debounce = <F extends (...args: any[]) => void>(
  func: F,
  delay: number
) => {
  let timer: ReturnType<typeof setTimeout> | null = null;
  return function (this: any, ...args: Parameters<F>) {
    clearTimeout(timer as ReturnType<typeof setTimeout>);
    timer = setTimeout(() => {
      func.apply(this, args);
    }, delay);
  };
};

export { throttle, debounce };
